package com.person.commons.utils;


import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

public class SendEmail {

	
	public static void send () throws Exception {
		
		try {
			Properties ps = System.getProperties() ;
			final String username = "lonelylover@963.cn" ;
			final String password = "123456" ;
			ps.put ("mail.smtp.host", "61.183.11.198") ;
			ps.put ("mail.smtp.auth", true) ;
			
			Session session = Session.getInstance(ps, new Authenticator () {
				public PasswordAuthentication getPasswordAuthentication () {
					return new PasswordAuthentication (username, password) ;
				}			
			} ) ;
			
			session.setDebug(true) ;
			
			MimeMessage msg = new MimeMessage (session) ;
			
			msg.setFrom(new InternetAddress ("lonelylover@963.cn")) ;
			
			msg.addRecipient(MimeMessage.RecipientType.TO, new InternetAddress ("zhangjie@wh.gov.cn")) ;
			
			msg.setSubject(encodeChineseText("会员注册激活")) ;
			
			MimeMultipart mp = new MimeMultipart() ;
			MimeBodyPart mbp = new MimeBodyPart() ;
			mbp.setContent("请激活您的会员帐号!", "text/html;charset=gbk") ;
			mp.addBodyPart(mbp) ;
			
			msg.setContent(mp) ;
			
			msg.setSentDate(new Date()) ;
			
			Transport.send(msg) ;
			
		} catch (Exception e) {
			e.printStackTrace() ;
		} 
		
	}
		
	
	
	private static String encodeChineseText(String text) throws UnsupportedEncodingException{
		return MimeUtility.encodeText(new String(text.getBytes(), "gb2312"), "gb2312", "B");
	}	
	
	
	
}
